<?php

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/init.php
#	FUNCTION:		Page initialisation
#	AUTHOR:			Cameron Morrow
#	CREATED:		26/07/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# P H P   S E T T I N G S
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Report all errors
error_reporting(E_ALL);

# Start session
session_start();

# Use this for IE to remember form data correctly
header("Cache-control: private");


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# F E E D B A C K
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Messages Array
$MESSAGE_ARRAY = array();

# Error Array
$ERROR_ARRAY = array();

# SQL Commands Array
$SQL_ARRAY = array();


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# D A T A B A S E   C O N N E C T I O N
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Attempt to establish connection
establishDatabaseConnection();

# Determine page ID
$PAGE_ID = substr($SCRIPT_NAME, strrpos($SCRIPT_NAME, "/") + 1);
$PAGE_ID = substr($PAGE_ID, 0, strrpos($PAGE_ID, "."));


# Determine site ID
$SITE_ID = substr($SCRIPT_NAME, 0, strrpos($SCRIPT_NAME, "/"));
$SITE_ID = substr($SITE_ID, strrpos($SITE_ID, "/") + 1);


# Create Pages object and provide with basic details of all pages
$PAGES = getAllPages();


# Get full data about current page
$PAGE = getPageDetails($PAGE_ID);

$XML1 = new Xmls();

if(!@$_GET["xmlstatusmode"]){//if xml status mode is not empty, load the xml
	
	LoadXML();
}


# Initialise User
$USER = new User();

# Page name
$PAGE_NAME = $PAGE -> getData("p_name");

# Is page compact
$PAGE_COMPACT = ($PAGE -> getData("p_popup") == "1");

# Page Content
$PAGE_CONTENT = "";

# Get mode from POST
$PAGE_MODE = (@$_POST["mode"]) ? (string) $_POST["mode"] : "";

# Get ID from POST
$PASSED_ID = (@$_POST["id"]) ? removeInvalidChars(strtolower($_POST["id"]), $VALID_CHAR_LIST["ids"]) : "";

# Get Search from POST
$SEARCH_TERM = (@$_POST["search"]) ? removeInvalidChars(strtolower($_POST["search"]), $VALID_CHAR_LIST["idsspaces"]) : "";

# Get Search Filter Value from POST
$SEARCH_FILTER = (@$_POST["filter"]) ? removeInvalidChars(strtolower($_POST["filter"]), $VALID_CHAR_LIST["names"]) : "";

# Authorized to view page?
$IS_AUTHORIZED = false;

# Authorization Message is the message to display
$AUTHORIZATION_MESSAGE = "<p>Please enter your user ID and password to continue.</p>";

# Instantiate Cart
$CART = new Cart();

function LoadXML(){

	global $XML1;
	
	global $PAGE_ID;
	
	if(@$_GET['xmlmode'] && @$_GET['xmlmode'] == "filter"){
			
		if(@$_GET["language"]){
			
			if(@$_GET["xmlmodule"]){
				
				$XML1 = getAllXmlTags($_GET["language"],$_GET["xmlmodule"],"");
			}
		}
	}
	else if(@$_GET['xmlmode'] && @$_GET['xmlmode'] == "section"){
		
		if(@$_GET["language"]){
			
			if(@$_GET["xmlsection"]){
				
				$XML1 = getAllXmlTags($_GET["language"],"",$_GET["xmlsection"]);
			}
		}
	}	
}

/**
*htmlChangePageStatus() generate the html code which is to change the status of page tags of xml
*@param $command is the command to decide which operation should be done in this function
*@param $siteID is the id of current site
*@param $titleID is the id of title
*@param $section is current section
*@return hyperlink which can trigger the function to change tag status
*/
function htmlChangePageStatus($command = '',$siteID = '',$titleID = '',$section = ''){
	
	$output = "";
	
	$txtHyperLink = "";
	
	switch($command){
		
		case 'locking':$txtHyperLink = 'Lock';break;
		case 'unlocking':$txtHyperLink = 'Unlock';break;
		case 'submitting':$txtHyperLink = 'Submit';break;
		case 'redoing':$txtHyperLink = 'Redo';break;
		case 'approving':$txtHyperLink = 'Approve';break;
		case 'unapproving':$txtHyperLink = 'Unapprove';break;
	}
	
	$output .= "<a href=\"javascript:void(0)\" onclick=\"onclick_changeTagStatus('".$command."','".$siteID."','".$titleID."','".$section."')\">".$txtHyperLink."</a>";
	
	return $output;
}

/**
*htmlPageComment() generate the html code related to page comment 
*@param $currentTag is the current selected tag
*@param $flag is to judge whether is editable or not
*@param $titleID is the id of title
*
*/
function htmlPageComment($currentTag,$flag = false){
	
	if($flag == true){
		
		addContent("<div id=\"dvPageComment\"><b>Page Comment:</b><br/><textarea readonly=\"readonly\" cols=\"60\" rows=\"10\">".$currentTag -> getData('x_comment')."</textarea><br/><a href= \"javascript:void(0)\" onclick = \"onclick_editpagecomment('".$currentTag->getID()."','".$currentTag -> getData('x_section')."')\">Edit</a></div>");
	}
	else{
		
		addContent("<div id=\"dvPageComment\"><b>Page Comment:</b><br/><textarea readonly=\"readonly\" cols=\"60\" rows=\"10\">".$currentTag -> getData('x_comment')."</textarea>");
	}

}

/**
*htmlTitleComment() generate the html code related to title comment
*@param $currentTag is the current selected tag
*@param $flag is to judge whether is editable or not
*
*/
function htmlTitleComment($currentTag,$flag =false,$titleID = ""){
	
	global $XML1;
	
	if(@$obTagTitle = $XML1 -> getSiteChildrenByName($currentTag->getID(),"title")){
				
		addContent ("<div id= \"dvTitle\">");
		
		$attParameter = $obTagTitle -> GetAttibuteParameterByKey('value');
		
		$EnglishId = getEnglishID($attParameter['at_id']);
		
		$arrEngAtr = getAtrByID($EnglishId);
		
		addContent ("<b>Title:</b><br/>".$arrEngAtr[0]['at_value']."(EN)<br/>".$obTagTitle -> GetAttributeByKey("value")."<br/>");

		addContent ("<b>Title Comment:</b><br/><textarea readonly=\"readonly\" cols=\"60\" rows=\"10\">".$attParameter['at_comment']."</textarea>");
		
		if($flag == true){
			
			addContent ("<br/>");
			
			addContent ("<a href=\"javascript:void(0)\" onclick = \"onclick_titleEditor('".$currentTag->getID()."','value','".$titleID."','".$currentTag->getData('x_section')."')\">Edit</a>");
		}

		addContent ("</div>");
	}
}

/**
*htmlInteraction() generate the html code related to interaction
*
*
*
*/
function htmlInteraction($currentTag,$flag = 1){
	
	addContent ("<div id=\"dvInteraction\">");
	
	switch($flag){
		
		case 1:
		
			if(@$currentTag -> getData('x_interaction')){// if current x_interaction is not empty
				
				$arrEngTag = getTagByID(getEnglishID($currentTag -> getID()));
		
				addContent ("<b>Interaction:</b><br/>".$arrEngTag[0]['x_interaction']."(EN)<br/>".$currentTag -> getData('x_interaction')."");
			}
			break;
				
		case 2:
		
			if(@$currentTag -> getData('x_interaction')){
				
				$arrEngTag = getTagByID(getEnglishID($currentTag -> getID()));
				
				addContent ("<b>Interaction:</b><br/>".$arrEngTag[0]['x_interaction']."(EN)<br/>".$currentTag -> getData('x_interaction')."<br/>");
				
				addContent ("<a href =\"javascript:void(0)\" onclick=\"onclick_editInteraction('".$currentTag->getID()."','".$currentTag->getData('x_section')."')\">Edit Interaction</a>");
			}
			break;
			
		case 3: 
		
			if($currentTag -> getData('x_language')=='en'){//if current tag`s language is english			

				if(@$currentTag -> getData('x_interaction')){//if current x_interaction is not empty
	
					addContent ("<b>Interaction:</b><br/>".$currentTag -> getData('x_interaction')."<br/>");
		
					addContent ("<a href =\"javascript:void(0)\" onclick=\"onclick_editInteraction('".$currentTag->getID()."','".$currentTag->getData('x_section')."')\">Edit Interaction</a>");
				}
				else{//if x_interaction is empty
		
					addContent ("<a href=\"javascript:void(0)\" onclick=\"onclick_addInteraction('".$currentTag->getID()."','".$currentTag -> getData('x_section')."')\">Add Interaction</a>");
				}
			}
			else{//if current language is not english
	
				if(@$currentTag -> getData('x_interaction')){
					
					$arrEngTag = getTagByID(getEnglishID($currentTag -> getID()));

					addContent ("<b>Interaction:</b><br/>".$arrEngTag[0]['x_interaction']."(EN)<br/>".$currentTag -> getData('x_interaction')."");
				}
			}
			break;
	}
	
	addContent ("</div>");
	
}

function refreshPageSiteTree(){
	
	global $XML1;
	
	global $USER;
	
//	if(@$_GET["language"]){
//	
//		$currentxmlstatus = "";
//	
//		if(@$_GET['xmlstatus']){
//			
//			$currentxmlstatus = $_GET['xmlstatus'];
//		}
//		
//		$obTagsSiteChildren = $XML1 -> getSiteChildrenOfTag($_GET["language"]."(root)");
//	
//		
//		addContent("<table width=\"100%\" id=\"tbTagsTree\"><tr><td>");
//		
//		addContent("<table>");
//		
//		foreach ($obTagsSiteChildren -> getTagIDs() as $obTagsId) {
//			
//			# Get list of groups authorized to view this tag
//			$arrTagAuthorized = $XML1 -> getAuthorizedGroups($obTagsId);
//			
//			# Get List of groups of users
//			$arrGroupAuthorized= explode(",", $USER -> getGroup());
//			
//			# Ensure it can be viewed at current authorization
//			$flagTagAuthorized = (findArrayInArray($arrTagAuthorized, $arrGroupAuthorized) !== false);
//			
//			if($flagTagAuthorized){
//			
//				$obTagStatus = $XML1 -> getXMLStatus($obTagsId);
//		
//				$obTag  = $obTagsSiteChildren -> getTag($obTagsId);
//				
//				if($currentxmlstatus != ""){
//				
//					if($currentxmlstatus == $obTagStatus){
//					
//						//addContent("test:".$obTag -> getData("x_name")."<br/>");
//						if(@$obTagTitle = $XML1 -> getSiteChildrenByName($obTag->getID(),"title")){
//							
//							$currentTitle =  $obTagTitle -> GetAttributeByKey("value");
//						
//							addContent("<tr><td valign=\"top\" width=\"20%\"><a href=\"javascript:void(0)\" onclick=\"onlick_openSiteParent(this,'".$obTag->getID()."','".$obTag->getData('x_section')."')\" style=\"text-decoration:none;\">+</a></td><td><a href=\"javascript:void(0)\" onclick=\"onclick_showTitle(this,'".$obTag->getID()."','".$obTagTitle -> getData("x_id")."' )\" title=\"".$currentTitle."\" style=\"text-decoration:none;\">".$currentTitle."</a><div></div></td></tr>");
//						}
//						else{
//							
//							addContent("<tr><td valign=\"top\" width=\"20%\"><a href=\"javascript:void(0)\" onclick=\"onlick_openSiteParent(this,'".$obTag->getID()."','".$obTag->getData('x_section')."')\" style=\"text-decoration:none;\">+</a></td><td><a href=\"javascript:void(0)\" onclick=\"onclick_showTitle(this,'".$obTag->getID()."','' )\" style=\"text-decoration:none;\">".$obTag -> getData("x_name")."</a><div></div></td></tr>");
//						}
//					}
//				}
//				else{
//					
//					if(@$obTagTitle = $XML1 -> getSiteChildrenByName($obTag->getID(),"title")){
//						
//						$currentTitle =  $obTagTitle -> GetAttributeByKey("value");
//						
//						addContent("<tr><td valign=\"top\" width=\"20%\"><a href=\"javascript:void(0)\" onclick=\"onlick_openSiteParent(this,'".$obTag->getID()."','".$obTag->getData('x_section')."')\" style=\"text-decoration:none;\">+</a></td><td><a href=\"javascript:void(0)\" onclick=\"onclick_showTitle(this,'".$obTag->getID()."','".$obTagTitle -> getData("x_id")."' )\" title=\"".$currentTitle."\" style=\"text-decoration:none;\">".$currentTitle."</a><div></div></td></tr>");
//					}
//					else{
//							
//						addContent("<tr><td valign=\"top\" width=\"20%\"><a href=\"javascript:void(0)\" onclick=\"onlick_openSiteParent(this,'".$obTag->getID()."','".$obTag->getData('x_section')."')\" style=\"text-decoration:none;\">+</a></td><td><a href=\"javascript:void(0)\" onclick=\"onclick_showTitle(this,'".$obTag->getID()."','' )\" style=\"text-decoration:none;\">".$obTag -> getData("x_name")."</a><div></div></td></tr>");
//					}
//				}
//			}
//		}
//		
//		addContent("</table>");
//		
//		addContent("</td><td valign=\"top\" align=\"left\" width=\"80%\"><div id=\"dvContentPanel\" align=\"left\"></div></td></tr></table>");
//	}
	
	$currentxmlstatus = "";
	
	if(@$_GET['xmlstatus'])	
		$currentxmlstatus = $_GET['xmlstatus'];
	
	$obTagsSiteChildren = $XML1 -> getSiteChildrenOfTag($_GET["language"]."(root)");

	addContent("<table width=\"100%\" id=\"tbTagsTree\"><tr><td valign=\"top\">");
	
	addContent("<table>");
	
	foreach ($obTagsSiteChildren -> getTagIDs() as $obTagsId) {
		
		# Get list of groups authorized to view this tag
		$arrTagAuthorized = $XML1 -> getAuthorizedGroups($obTagsId);
		
		# Get List of groups of users
		$arrGroupAuthorized= explode(",", $USER -> getGroup());
		
		# Ensure it can be viewed at current authorization
		$flagTagAuthorized = (findArrayInArray($arrTagAuthorized, $arrGroupAuthorized) !== false);
		
		if($flagTagAuthorized){
		
			$obTagStatus = $XML1 -> getXMLStatus($obTagsId);
	
			$obTag  = $obTagsSiteChildren -> getTag($obTagsId);
			
			if($currentxmlstatus != ""){
				
				if($currentxmlstatus != "@all"){
			
					if($currentxmlstatus == $obTagStatus){
					
						if(@$obTagTitle = $XML1 -> getSiteChildrenByName($obTag->getID(),"title")){
							
							$pageTitle = $obTagTitle -> GetAttributeByKey("value");//current title of page
		
							addContent("<tr><td valign=\"top\" width=\"20%\"><a href=\"javascript:void(0)\" onclick=\"onlick_openSiteParent(this,'".$obTag->getID()."','".$obTag->getData('x_section')."')\" style=\"text-decoration:none;\">+</a></td><td><a href=\"javascript:void(0)\" onclick=\"onclick_showTitle(this,'".$obTag->getID()."','".$obTagTitle -> getData("x_id")."','".$obTag->getData('x_section')."')\" title=\"".$pageTitle."\" style=\"text-decoration:none;\">".getXmlStatusIcon($obTagStatus).$pageTitle."</a><div></div></td></tr>");
						}
						else{
							
							addContent("<tr><td valign=\"top\" width=\"20%\"><a href=\"javascript:void(0)\" onclick=\"onlick_openSiteParent(this,'".$obTag->getID()."','".$obTag->getData('x_section')."')\" style=\"text-decoration:none;\">+</a></td><td><a href=\"javascript:void(0)\" onclick=\"onclick_showTitle(this,'".$obTag->getID()."','','".$obTag->getData('x_section')."')\" style=\"text-decoration:none;\">".getXmlStatusIcon($obTagStatus).$obTag -> getData("x_name")."</a><div></div></td></tr>");
						}
					}
				}
				else{
					
					if(@$obTagTitle = $XML1 -> getSiteChildrenByName($obTag->getID(),"title")){
							
						$pageTitle = $obTagTitle -> GetAttributeByKey("value");//current title of page
		
						addContent("<tr><td valign=\"top\" width=\"20%\"><a href=\"javascript:void(0)\" onclick=\"onlick_openSiteParent(this,'".$obTag->getID()."','".$obTag->getData('x_section')."')\" style=\"text-decoration:none;\">+</a></td><td><a href=\"javascript:void(0)\" onclick=\"onclick_showTitle(this,'".$obTag->getID()."','".$obTagTitle -> getData("x_id")."','".$obTag->getData('x_section')."')\" title=\"".$pageTitle."\" style=\"text-decoration:none;\">".getXmlStatusIcon($obTagStatus).$pageTitle."</a><div></div></td></tr>");
					}
					else{
							
						addContent("<tr><td valign=\"top\" width=\"20%\"><a href=\"javascript:void(0)\" onclick=\"onlick_openSiteParent(this,'".$obTag->getID()."','".$obTag->getData('x_section')."')\" style=\"text-decoration:none;\">+</a></td><td><a href=\"javascript:void(0)\" onclick=\"onclick_showTitle(this,'".$obTag->getID()."','','".$obTag->getData('x_section')."')\" style=\"text-decoration:none;\">".getXmlStatusIcon($obTagStatus).$obTag -> getData("x_name")."</a><div></div></td></tr>");
					}
				}
			}
		}
	}
	
	addContent("</table>");
	
	addContent("</td><td valign=\"top\" align=\"left\" width=\"80%\"><div id=\"dvContentPanel\" align=\"left\"></div></td></tr></table>");
}

function generateDropdownlistGroups (){
	
	global $USER;
	
	$output = "";
	
	$arrLanguages =  getExistLans();
				
	$output .= "<select id=\"selLan\" onchange=\"onchange_chooseLanguage()\" style=\"width:150px;\">";
	
	$output .= "<option value='@clearing' onclick=\"onchange_chooseLanguage()\">Loading...</option>";
				
	foreach($arrLanguages as $valueLanguages){

		if($USER -> IsUserAuthorisedForLanguage($valueLanguages['x_language'])){
							
			$output .= "<option value='".$valueLanguages['x_language']."' onclick=\"onchange_chooseLanguage()\">".$valueLanguages['lan_name']."</option>";
		}
	}
				
	$output .= "</select>";
	
	$output .= "<br/><br/>";
	
	$output .= "<select id=\"selModules\" onchange=\"onchange_chooseModule()\" style=\"width:150px;\">";
	
	$output .= "</select>";
	
	$output .= "<br/><br/>";
	
	$output .= "<select id=\"selStatus\" onchange=\"onchange_triggerLanguageFilter()\" style=\"width:150px;\">";
	
	$output .= "</select>";
	
	$output .= "<br/><br/>";
	
	$output .= "<div id=\"dvRefreshButton\"></div>";
	
	return $output;
					
//	$arrModules = getXMLModules();
//			
//	addContent ("<select id=\"selModules\" onchange=\"onchange_chooseModule()\" style=\"width:150px;\">");
//				
//	foreach($arrModules as $valueModules){
//					
//		if($valueModules['x_module'] == ""){
//						
//			addContent("<option value='no module' onclick=\"onchange_chooseModule()\">no module</option>");
//		}
//		else{
//			
//			addContent("<option value='".$valueModules['x_module']."' onclick=\"onchange_triggerLanguageFilter()\">".$valueModules['x_module']."</option>");			
//		}
//	}
//				
//	addContent("</select>");
//
//	addContent("<br/><br/>");
//				
//	addContent("<select id=\"selStatus\" onchange=\"onchange_triggerLanguageFilter()\" style=\"width:150px;\">");
//				
//	addContent("<option value=\"\" onclick=\"onchange_triggerLanguageFilter()\">All</option>");
//				
//	addContent("<option value=\"1\" onclick=\"onchange_triggerLanguageFilter()\">Pending</option>");
//				
//	addContent("<option value=\"2\" onclick=\"onchange_triggerLanguageFilter()\">Locked</option>");
//				
//	if($USER -> IsNameInGroup("Administrators") || $USER -> IsNameInGroup("Developers")){
//					
//		addContent("<option value=\"3\" onclick=\"onchange_triggerLanguageFilter()\">Unapproved</option>");
//				
//		addContent("<option value=\"4\" onclick=\"onchange_triggerLanguageFilter()\">Approved</option>");		
//	}
//				
//	addContent("</select>");			
}

?>